package com.weirong.smartvault.backservice.util.common;

import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.log4j.Logger;

import java.sql.Timestamp;
import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * 于2017/11/28由 KFEB4 创建 .
 */
public class DateUtil {
    public static Logger LOGGER = Logger.getLogger(DateUtil.class);

    private static final FastDateFormat FORMAT_NORMAL_STR = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

    private static final FastDateFormat FORMAT_NO_SIGN_STR = FastDateFormat.getInstance("yyMMddHHmmss");

    private static final FastDateFormat FORMAT_NORMAL_DATE_STR = FastDateFormat.getInstance("yyyy-MM-dd");

    private static final FastDateFormat FORMAT_MONTH_STR = FastDateFormat.getInstance("yyyy-MM");

    private static final String TIME_BEGIN = " 00:00:00";

    private static final String TIME_END = " 23:59:59";

    private static final String YEAR_BEGIN = "-01-01"+TIME_BEGIN;

    private static final String YEAR_END = "-12-31"+TIME_END;

    private static final String MONTH_BEGIN = "-01"+TIME_BEGIN;


    public static Boolean compareDateInUnit(Date d1,Date d2,Integer unit){
        //比较两个日期，如果相差时间在单位内，则返回True,否则返回False
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        Integer t1 = c1.get(unit);
        Integer t2 = c2.get(unit);
        return t1.equals(t2);
    }
    public static Date getYearStart(String year) throws ParseException {
        return FORMAT_NORMAL_STR.parse(year+YEAR_BEGIN);
    }
    public static Date getYearEnd(String year) throws ParseException {
        return FORMAT_NORMAL_STR.parse(year+YEAR_END);
    }
    public static Date getMonthStart(Integer year, Integer month) throws ParseException {
        String express = year.toString()+"-"+ String.format("%02d",month)+MONTH_BEGIN;
        return FORMAT_NORMAL_STR.parse(express);
    }
    public static Date getMonthEnd(Integer year, Integer month) throws ParseException { //好像有问题
        return getMonthStart(year,month+1);
    }

    public static Date getDayStart(Integer year,Integer month,Integer day) throws ParseException {
        String express = year.toString()+"-"+ String.format("%02d-%02d",month,day)+TIME_BEGIN;
        return FORMAT_NORMAL_STR.parse(express);
    }
    public static Date getDayEnd(Integer year,Integer month,Integer day) throws ParseException {
        return getDayStart(year,month,day+1);
    }

    public static Date getNormalDate(String date) throws ParseException {
        return FORMAT_NORMAL_STR.parse(date);
    }
    public static Date getTodayStart() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        return  getDayStart(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1,calendar.get(Calendar.DAY_OF_MONTH));
    }
    public static Date getTodayEnd() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        return  getDayEnd(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1,calendar.get(Calendar.DAY_OF_MONTH));
    }

    public static Date getMonthDate(String date) throws ParseException {
        return FORMAT_MONTH_STR.parse(date);
    }
    public static String getNomalString(Date date){
        //返回标准化时间
        if (date==null)
            return "";
        return FORMAT_NORMAL_STR.format(date);
    }

    public static Date getTodayOrParse(String date) throws ParseException {
        Date start;
        if (date.length()==0){
            start = getBeginDate(new Date());
        }
        else{
            start = FORMAT_NORMAL_DATE_STR.parse(date);
        }
        return start;
    }
    public static Date getDateNext(Date date){
        Calendar  calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.roll(Calendar.DAY_OF_YEAR,1);
        return calendar.getTime();
    }


    public static String getNoSignString(Date date) {
        return FORMAT_NO_SIGN_STR.format(date);
    }

    public static Date getNowDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day);
        return c.getTime();
    }

    public static Date getBeginDate(Date date) {
        return getDate(date,TIME_BEGIN);
    }

    public static Date getEndDate(Date date) {
        return getDate(date,TIME_END);
    }

    private static Date getDate(Date date,String time){
        Date nowDate = getNowDate(date);
        String dateStr = FORMAT_NORMAL_DATE_STR.format(nowDate);
        Date endPMDate = null;
        try {
            endPMDate = FORMAT_NORMAL_STR.parse(dateStr + time);
        } catch (ParseException e) {
            LOGGER.error(e.getMessage(), e);
        }
        return endPMDate;

    }
    public static Timestamp getBeginTime(Date date) {
        return new Timestamp(getBeginDate(date).getTime());
    }

    public static Timestamp getEndTime(Date date) {
        return new Timestamp(getEndDate(date).getTime());
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

    public static String getThisMonth() {
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        Integer month = calendar.get(Calendar.MONTH)+1;
        return year.toString()+"-"+ String.format("%02d",month);
    }

    public static String getThisYear() {
        return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
    }
}
